package com.xio.cloud.service.impl;

import com.xio.cloud.service.AccountService;
import com.xio.cloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xio.cloud.domain.OrderForm;
import com.xio.cloud.mapper.OrderFormMapper;
import com.xio.cloud.service.OrderFormService;

import javax.annotation.Resource;

/**
 * @Author: xio
 * @Description: ${Description}
 * @Date: 2021/3/31 15:35
 */
@Slf4j
@Service
public class OrderFormServiceImpl
        extends ServiceImpl<OrderFormMapper, OrderForm>
        implements OrderFormService {

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "global-create-order", rollbackFor = Exception.class)
    public boolean createOrder(OrderForm orderForm) {

        log.info("- - - - - -> seata-order-service: 开始新建订单");
        save(orderForm);

        log.info("- - - - - -> seata-order-service: 减少库存开始");
        storageService.decrease(orderForm.getProductId(), orderForm.getCount());
        log.info("- - - - - -> seata-order-service: 减少库存结束");

        log.info("- - - - - -> seata-order-service: 减少账户余额开始");
        accountService.decrease(orderForm.getUserId(), orderForm.getMoney());
        log.info("- - - - - -> seata-order-service: 减少账户余额结束");

        log.info("- - - - - -> seata-order-service: 改订单状态");
        orderForm.setStatus(Boolean.TRUE);
        boolean result = updateById(orderForm);
        log.info("- - - - - -> seata-order-service: 新建订单完成");

        return result;
    }

}
